package com.c.ctgproject.adapter

import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView


import com.bumptech.glide.Glide
import com.c.ctgproject.BuildConfig
import com.c.ctgproject.R
import com.c.ctgproject.ui.other.ShowImgVideoActivity
import com.c.ctgproject.utils.GlideEngine
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.wiget.dialog.PhotoAlbumDialog
import com.huantansheng.easyphotos.EasyPhotos
import com.huantansheng.easyphotos.callback.SelectCallback
import com.huantansheng.easyphotos.models.album.entity.Photo
import org.jetbrains.anko.startActivity
import java.io.File


class PhotoShowListAdapterV2(
    private val context: FragmentActivity,
    private val mList: ArrayList<String>,
    private val max: Int,
    private val isEditable: Boolean,
    private val listener: PhotoShowListCallBack,
//
//    private val parentPosition: Int
) : RecyclerView.Adapter<PhotoShowListAdapterV2.MyViewHolder>(), PhotoAlbumDialog.Callback {


    fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            if (isEditable) {
                if (position < mList.size) {
                    Glide.with(context).load(mList[position]).placeholder(R.drawable.app_icon).into(holder.mImageView)
                    holder.mDeleteIV.setOnClickListener {
                        mList.remove(mList[position])
//                        listener.deleteItem(mList[position], parentPosition)
                        notifyDataSetChanged()
                    }
                    holder.mDeleteIV.visibility = View.VISIBLE
                    holder.mImageView.setOnClickListener {

//                        ShowImgVideoFragment(
//                            mList[position],
//                            false
//                        ).show(context.supportFragmentManager, "")
//                        ShowImgVideoListFragment(
//                            mList,
//                        ).show(context.supportFragmentManager, "")
                        context.startActivity<ShowImgVideoActivity>("urls" to getGson().toJson(mList),"position" to position)
                    }
                } else {
                    Glide.with(context).load(R.drawable.ic_ngv_add_pic).into(holder.mImageView)
                    holder.mImageView.setOnClickListener {

                        //context.startActivity<TakePhotoToDealActivity>("parentPosition" to parentPosition)
                        // context.startActivity<ImageDataActivity>("parentPosition" to parentPosition)
                        //choosePhoto()
                        PhotoAlbumDialog(this).show(context.supportFragmentManager, "")
                    }
                    holder.mDeleteIV.visibility = View.INVISIBLE
                }
            } else {
                if (position < mList.size) {
                    Glide.with(context).load(mList[position]).placeholder(R.drawable.app_icon).into(holder.mImageView)

                    holder.mDeleteIV.visibility = View.INVISIBLE
                    holder.mImageView.setOnClickListener {

//                        ShowImgVideoFragment(
//                            mList[position],
//                            false
//                        ).show(context.supportFragmentManager, "")
//                        ShowImgVideoListFragment(
//                            mList,
//                        ).show(context.supportFragmentManager, "")
                        context.startActivity<ShowImgVideoActivity>("urls" to getGson().toJson(mList),"position" to position)
                    }
                } else {
//                    Glide.with(context).load(R.mipmap.bg_add_photo_grey).into(holder.mImageView)
//                    holder.mImageView.setOnClickListener {
//                        context.startActivity<TakePhotoToDealActivity>()
//                    }
                    //holder.parentLL.visibility=View.GONE
                    holder.mDeleteIV.visibility = View.GONE
                    //holder.mImageView.visibility = View.GONE
                }
            }

//            holder.mImageView.setOnClickListener {
//                if(position<mList.size-1){
//                    ShowImgVideoFragment(mList[position],false).show(context.supportFragmentManager, "")
//                }
//            }
        }

    }

    fun takePhoto() {
        EasyPhotos.createCamera(context, true) //参数说明：上下文
            .setFileProviderAuthority(BuildConfig.FILE_PROVIDER)
            .start(object : SelectCallback() {
                override fun onResult(photos: ArrayList<Photo>, isOriginal: Boolean) {
                    //获取file,进行对应操作
                    val file = File(photos[0].path)
                    //startForDeal(file)
                    listener.choosePhotoItem(arrayListOf(photos[0].path))
                }

                override fun onCancel() {}
            })
    }

    fun choosePhoto() {
        val num=if((max - mList.size)>6) 6 else max - mList.size
        EasyPhotos.createAlbum(context, false, true, GlideEngine.getInstance())
            .setCount(num)
            .start(object : SelectCallback() {
                override fun onResult(photos: ArrayList<Photo>, isOriginal: Boolean) {
                    //获取file，进行对应操作
                    //val file = File(photos[0].path)
                    val photoPaths = ArrayList<String>().apply {
                        photos.forEach {
                            this.add(it.path)
                        }
                    }
                    listener.choosePhotoItem(photoPaths)
                    //MyLog.d(file.absolutePath)
                }

                override fun onCancel() {}
            })

    }

    fun startForDeal(file: File) {
//        val intent = Intent(context, IMGEditActivity::class.java)
//        val uri = Uri.fromFile(file);
//        //mImageFile = File(context.getExternalCacheDir()!!.getPath(), UUID.randomUUID().toString() + ".jpg")
//        intent.putExtra(IMGEditActivity.EXTRA_IMAGE_URI, uri)
//        intent.putExtra(IMGEditActivity.EXTRA_IMAGE_SAVE_PATH, file.absolutePath)
//        intent.putExtra(IMGEditActivity.EXTRA_IMAGE_CONFORM_TITLE, "完成")
//        intent.putExtra("parentPosition", parentPosition)
//        //val currentChartActivity:CurrentChartActivity=context as CurrentChartActivity
//        //currentChartActivity.test(intent)
//        context.startActivityForResult(
//            intent,
//            1
//        )
    }

     class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mImageView: ImageView = view.findViewById(R.id.mImageView)
        val mDeleteIV: ImageView = view.findViewById(R.id.mDeleteIV)
         val parentLL: LinearLayoutCompat=view.findViewById(R.id.parentLL)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view =
            LayoutInflater.from(context).inflate(R.layout.item_show_photo_list, parent, false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        handlerData(holder, position)
    }

    override fun getItemCount(): Int {
        if (max == mList.size) {
            return mList.size
        } else {
            return mList.size + 1
        }
    }

    interface PhotoShowListCallBack {
        fun deleteItem(url: String, position: Int)
        fun choosePhotoItem(urls: List<String>)
    }

    override fun closeSelect(selection: Int) {
        when (selection) {
            0 -> takePhoto()
            1 -> choosePhoto()
        }
    }
}